<template>
  <div class="app-container">
<!--  通用题库规则  -->
    <el-form ref="form" :model="bankRuleForm" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目名称" prop="projectName">
            <el-input
                    v-model="bankRuleForm.projectName"
                    clearable
                    size="small"
                    :disabled="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="题库名称" prop="questionBankName">
            <el-input
                    v-model="bankRuleForm.questionBankName"
                    placeholder="请输入题库名称"
                    clearable
                    size="small"
                    :disabled="true"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-table :data="bankRuleForm.bankRuleList" border max-height="350">
            <el-table-column label="题型" align="center" :formatter="questionTypeFormat" width="70" prop="questionType"/>
            <el-table-column label="数量" align="center" prop="quantity">
              <template slot-scope="scope">
                <el-input-number controls-position="right"
                                 :min="0" :max="30" size="large"
                                 v-model="scope.row.quantity" clearble/>
              </template>
            </el-table-column>
            <el-table-column label="分值" align="center" prop="score">
              <template slot-scope="scope">
                <el-input-number controls-position="right"
                                 :min="0" :max="10" size="large"
                                 v-model="scope.row.score" clearble/>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
    </el-form>
    <div class="dialog-footer" style="float:right;margin-top: 10px;">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm('submit')">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </div>
</template>

<script>
  import {addBankRule} from "@/api/question/questionBank";

  export default {
    name: "blanketRuleForm",
    props: {
      // 题库规则表单信息
      bankRuleForm: Object,
    },
    data() {
      return {
        // 按钮loading
        buttonLoading: false,
        // 试题类型数据字典
        questionTypeOptions: [],
      }
    },
    created() {
      //  初始化方法
      // console.log("=====" + JSON.stringify(this.bankRuleForm));
      //查询试题类别字典状态
      this.getDicts("question_type").then(response => {
        this.questionTypeOptions = response.responseBody.result;
      });
    },
    methods: {

      // 试题类型字典翻译
      questionTypeFormat(row, column) {
        return this.selectDictLabel(this.questionTypeOptions, row.questionType);
      },

      /** 提交按钮 */
      submitForm(flag) {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.buttonLoading = true;
            if (this.bankRuleForm.questionBankId != null) {
              // 校验表格
              const temp = this.verifyTable();
              if (temp !== '') {
                this.buttonLoading = false;
                return this.$message({
                  // title: '属性列表提示',
                  dangerouslyUseHTMLString: true,
                  type: 'warning',
                  duration:2000,
                  message: `<ul>${temp}</ul>`
                });
              }
              // console.log("提交的数据：" + JSON.stringify(this.bankRuleForm));
              addBankRule(this.bankRuleForm).then(response => {
                if (response.retCode === '200') {
                  this.msgSuccess(response.message);
                }
                // this.msgSuccess("保存成功！");
                if (flag === 'submit') {
                  // 关闭对话框
                  this.$emit("cancelDialog");
                  //刷新数据
                  this.$emit("submitForm")
                }
              }).finally(() => {
                this.buttonLoading = false;
              });
            } else {
              this.msgError("保存失败！");
              // 关闭对话框
              this.$emit("cancelDialog");
              this.buttonLoading = false;
            }
          }
        });
      },
      // 验证属性列表
      verifyTable() {
        var result = true
        var message = []
        this.bankRuleForm.bankRuleList.forEach((item,index) => {
          let no = index + 1
          if (item.quantity === undefined || item.quantity === 0) {
            result = result && false
            message.push('第'+no+'行：数量不能为空或0！')
          }
          if (item.score === undefined || item.score === 0) {
            result = result && false
            message.push('第'+no+'行：分值不能为空或0！')
          }
        })
        var temp = '';
        if (!result) {
          message.forEach(v => {
            temp += `<li>${v}</li>`;
          })
          //$notify
          /*return this.$message({
            // title: '属性列表提示',
            dangerouslyUseHTMLString: true,
            type: 'warning',
            duration:2000,
            message: `<ul>${temp}</ul>`
          });*/
          return temp;
        }
        return temp;
      },

      // 取消按钮
      cancel() {
        // 清空
        this.$emit("cancelDialog");
      },
    }
  }
</script>
